import 'dart:async';
import 'dart:convert';

import 'package:flutter/services.dart';

import 'permission_result.dart';

class Permission {
  static const MethodChannel _channel = MethodChannel('permission');

  // <uses-permission android:name="android.permission.CAMERA" />
  static var camera = "android.permission.CAMERA";

  // <uses-permission android:name="android.permission.RECORD_AUDIO" />
  static var record = "android.permission.RECORD_AUDIO";

  // <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  static var storage = "android.permission.READ_EXTERNAL_STORAGE";

  static Future<List<PermissionResult>> requestPermissions(
      List<String> permissions,
      {requestCode = 1001}) async {
    var result = await _channel
        .invokeMethod("requestPermissions", [permissions, requestCode]);
    print("result -- :" + result);
    return transToResultList(result);
  }

  static List<PermissionResult> transToResultList(String result) {
    List<PermissionResult> resultList = [];
    List mapList = jsonDecode(result);
    for (var element in mapList) {
      resultList.add(PermissionResult(
          element["requestCode"], element["permission"], element["grant"]));
    }
    return resultList;
  }
}
